package com.on.smile.controller;

import com.on.smile.entity.CustomerEntity;
import com.on.smile.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.List;

@Controller
@RequestMapping("/api/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    /**
     * 1.参数校验：就是对方法的参数的有无、是否合法进行校验
     * <p>
     * 2.操作失败的情况
     * <p>
     * 3.操作成功的情况
     *
     * @param customer
     */
    @PostMapping("/insert")
    @ResponseBody
    public String insertCustomer(@RequestBody CustomerEntity customer) {
        String username = customer.getUsername();
        String realName = customer.getRealName();
        Integer sex = customer.getSex();
        // 判断字符串的方法
        if (username == null || "".equals(username)) {
            return "用户名不可为空";
        } else if (realName == null || "".equals(realName)) {
            return "真实姓名不可为空";
        } else if (sex == null) {
            return "性别不可为空";
        } else if (sex != 0) {
            if (sex != 1) {
                if (sex != 2) {
                    return "性别不合法";
                }
            }
        }
        customerService.insertCustomer(customer);
        return "成功添加一条数据";
    }

    @PostMapping("/delete")
    @ResponseBody
    public String deleteCustomer(Long id) {

        // 参数校验
        if (id == null) {
            return "ID 不能为空";
        }

        int num = customerService.deleteCustomer(id);

        // 这里必须知道是否删除成功，必须要有一个判断依据才行

        if (num == 0) {
            return "删除失败";
        }

        return "删除成功";
    }

    @PostMapping("/deletes")
    @ResponseBody
    public String deleteCustomers(@RequestBody List<Long> id) {
        int size = id.size();
        if (size == 0) {
            return "删除失败";
        }
        int num = customerService.deleteCustomers(id);
        return "删除了" + num + "个数据";
    }

    @PostMapping("/update")
    @ResponseBody
    public String updateCustomer(@RequestBody CustomerEntity customer) {
        String username = customer.getUsername();
        String realName = customer.getRealName();
        Integer sex = customer.getSex();
        // 判断字符串的方法
        if (username == null || "".equals(username)) {
            return "用户名不可为空";
        } else if (realName == null || "".equals(realName)) {
            return "真实姓名不可为空";
        } else if ("".equals(sex)) {
            return "性别不可为空";
        } else if (sex == 0) {
            if (sex == 1) {
                if (sex == 2) {
                    customerService.updateCustomer(customer);
                    return "成功添加一条数据";
                }
            }
        }
        return "性别不合法";
    }

    @PostMapping("/selectPage")
    @ResponseBody
    public List<CustomerEntity> select(Long page, Long size) {
        if (page==null||size==null||page <= 0 || "".equals(page) || size <= 0 || "".equals(size)) {
            System.out.println("查询不合法");
        }
        return customerService.selectCustomerPage(page, size);
    }

    @PostMapping("/selectAll")
    @ResponseBody
    public List<CustomerEntity> selectAll() {
        return customerService.selectCustomerAll();
    }

}
